import org.molamil.acto.commands.CommandContext;
import org.molamil.acto.commands.factory.CommandFactory;
import org.molamil.acto.core.Logger;

/**
 * @author Jorge Hernandez
 */
class org.molamil.acto.commands.factory.BasicCommandFactory implements CommandFactory {
	
	
	// Properties
	
	private static var log:Logger = Logger.getLogger("BasicCommandFactory");
	
	private var _list:Array;
	
	
	// Constructor
	
	/**
	 * Creates an empty BasicCommandFactory.
	 */
	public function BasicCommandFactory(list:Array) {
		log.debug("Constructor");
		if (list != null) {
			_list = list;
		} else {
			_list = new Array();
		}
	}
	
	
	// Functions
	
	public function containsCommand(name:String):Boolean {
		var b:Boolean = getCommand(name) != null;
		log.debug("containsCommand(), name = " + name + ": " + b);
		return b;
	}
	
	public function getCommand(name:String):CommandContext {
		
		log.info("getCommand(), name = " + name);
		
		var p:CommandContext = null;
		
		for (var i:Number = 0; i < _list.length; i++) {
			var pt:CommandContext = _list[i];
			if (pt.name == name) {
				p = pt;
				break;
			}
		}
		
		if (p == null)
			log.warning("getCommand(), name = " + name + " not found");
			
		return p;
		
	}
	
	public function getCommands():Array {
		log.info("getCommands(), returning " + _list.length);
		return _list;
	}
	
	
}
